package cn.gzmli.dtt.common.controll;

import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.utils.Constant;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.service.log.UserOnlineLogService;
import cn.gzmli.dtt.common.utils.BaseUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Descript
 * @Author xsx
 * @Date 2018/3/23
 */
@Controller
@RequestMapping("/statistics/userOnline")
@ResponseBody
@Ignore
public class UserOnlineLogController {
    @Autowired
    private UserOnlineLogService userOnlineLogService;
    @RequestMapping("/listByCondition")
    public Result listByCondition(HttpServletRequest request, PageInfo pageInfo) throws Exception {
        Map<String, Object> condition = new HashMap<>();
        if (StrUtil.isNotBlank(request.getParameter("userIDS"))){
            String userIDS = request.getParameter("userIDS");
            condition.put(" AND userId IN("+ BaseUtil.getPlaceholder(userIDS)+")", BaseUtil.getArgs(userIDS, Integer.class));
        }
        if (StrUtil.isNotBlank(request.getParameter("loginName"))){
            condition.put(" AND loginName LIKE ?", "%"+request.getParameter("loginName")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("userName"))){
            condition.put(" AND userName LIKE ?", "%"+request.getParameter("userName")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("departmentIDS"))){
            String departmentIDS = request.getParameter("departmentIDS");
            condition.put(" AND departmentId IN("+ BaseUtil.getPlaceholder(departmentIDS)+")", BaseUtil.getArgs(departmentIDS, Integer.class));
        }
        if (StrUtil.isNotBlank(request.getParameter("departmentName"))){
            condition.put(" AND departmentName LIKE ?", "%"+request.getParameter("departmentName")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("beginTime"))){
            condition.put(" AND lastTime >= ?", request.getParameter("beginTime"));
        }
        if (StrUtil.isNotBlank(request.getParameter("endTime"))){
            condition.put(" AND lastTime <= ?", request.getParameter("endTime") + " 23:59:59");
        }
        List<Map<String, Object>> data = this.userOnlineLogService.getByCondition(condition, pageInfo);
        return new Result(Constant.SUCCESS, "获取用户在线统计列表成功", data, pageInfo);
    }

    @RequestMapping("/statisticsByTime")
    public Result statisticsByTime(HttpServletRequest request) throws Exception {
        Map<String, Object> condition = new HashMap<>();
        if (StrUtil.isNotBlank(request.getParameter("departmentIDS"))){
            String departmentIDS = request.getParameter("departmentIDS");
            condition.put(" AND departmentId IN("+ BaseUtil.getPlaceholder(departmentIDS)+")", BaseUtil.getArgs(departmentIDS, Integer.class));
        }
        if (StrUtil.isNotBlank(request.getParameter("departmentName"))){
            condition.put(" AND departmentName LIKE ?", "%"+request.getParameter("departmentName")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("beginTime"))){
            condition.put(" AND lastTime >= ?", request.getParameter("beginTime"));
        }
        if (StrUtil.isNotBlank(request.getParameter("endTime"))){
            condition.put(" AND lastTime <= ?", request.getParameter("endTime") + " 23:59:59");
        }

        List<Map<String, Object>> data = this.userOnlineLogService.getByTime(condition);
        return new Result(Constant.SUCCESS, "获取用户在线统计时长成功", data);
    }

    @RequestMapping("/statisticsByCount")
    public Result statisticsByCount(HttpServletRequest request) throws Exception {
        Map<String, Object> condition = new HashMap<>();
        if (StrUtil.isNotBlank(request.getParameter("departmentIDS"))){
            String departmentIDS = request.getParameter("departmentIDS");
            condition.put(" AND departmentId IN("+ BaseUtil.getPlaceholder(departmentIDS)+")", BaseUtil.getArgs(departmentIDS, Integer.class));
        }
        if (StrUtil.isNotBlank(request.getParameter("departmentName"))){
            condition.put(" AND departmentName LIKE ?", "%"+request.getParameter("departmentName")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("beginTime"))){
            condition.put(" AND lastTime >= ?", request.getParameter("beginTime"));
        }
        if (StrUtil.isNotBlank(request.getParameter("endTime"))){
            condition.put(" AND lastTime <= ?", request.getParameter("endTime") + " 23:59:59");
        }
        List<Map<String, Object>> data = this.userOnlineLogService.getByCount(condition);
        return new Result(Constant.SUCCESS, "获取用户在线统计人数成功", data);
    }


}
